package net.towee.server.ftp;

import javax.inject.Inject;

import net.towee.model.account.AccountData;
import net.towee.server.persistence.AccountStore;
import net.towee.server.persistence.AttachmentStore;
import net.towee.server.persistence.DiscussionStore;
import net.towee.server.persistence.DocumentStore;
import net.towee.shared.exceptions.PersistenceException;

import org.apache.ftpserver.ftplet.FileSystemFactory;
import org.apache.ftpserver.ftplet.FileSystemView;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.User;

/**
 * Factory of towee file system view
 * @author c58
 */
public class ToweeFileSystemFactory implements FileSystemFactory {
	private final AccountStore accountStore;
	private final AttachmentStore attStore;
	private final DocumentStore docStore;
	private final DiscussionStore discStore;

	@Inject
	public ToweeFileSystemFactory(AccountStore accountStore, AttachmentStore attStore, 
			DocumentStore docStore, DiscussionStore discStore){
		this.accountStore = accountStore;
		this.attStore = attStore;
		this.docStore = docStore;
		this.discStore = discStore;
	}
	
	@Override
	public FileSystemView createFileSystemView(User user) throws FtpException {
		try {
			// Try to get account
			AccountData acc = accountStore.getAccountById(user.getName(), "accountGroups");

			// Get groups where account entered and create filesystem vew
			return new ToweeFileSystemView( acc.getGroups().getGroupsList(), docStore, attStore, discStore );
		} catch (PersistenceException e) {
			throw new FtpException();
		}
	}

}
